/*
* Copyright 2010 Dovado FZ-LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import "DovadoAppDelegate.h"


@implementation DovadoAppDelegate

@synthesize window;
@synthesize tabBarController;
@synthesize aboutNavigationController;
@synthesize mLoginViewController;
@synthesize aboutRouterWebView;
@synthesize aboutClientWebView;
@synthesize aboutGeneralWebView;
@synthesize aboutLicenseWebView;
@synthesize mRouterLocation;
@synthesize mPassword;
@synthesize mUser;
@synthesize mHostAddress;
@synthesize mAliasArray;
@synthesize mGroupsArray;
@synthesize mInfoArray;
@synthesize router;
@synthesize justLoggedIn;

#if 0
- (void) assignImageToImageView:(UIImage *)img
{
	/*
	NSUInteger photoImageTag = 4;
	UITableViewCell *tcell = (UITableViewCell *)[tview viewWithTag:30];
	
	if (tcell != nil)
	{
		UIImageView *iview	= (UIImageView *)[tcell.contentView viewWithTag:photoImageTag];
		
		if (iview != nil)
		{
		    [iview setImage:img];
		}
	}
	*/
	
}

- (void) loadUIWebViewInBackground
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	/*
	// N.B. an instance of my 'Menu' class has been created, called 'menuItem'
	
	NSURL *imgURL     = [NSURL URLWithString:menuItem.photoPath];
	NSData *imgData   = [NSData dataWithContentsOfURL:imgURL];
	UIImage *img    = [[UIImage alloc] initWithData:imgData];
	*/
	
	/* TODO: Load the UIWebView here */
	[self performSelectorOnMainThread:@selector(assignImageToImageView:) withObject:img waitUntilDone:YES];
	
	/* [img release]; */	
	[pool release];
}
#endif

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
    mLoginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:NULL];
	CGRect frame = mLoginViewController.view.frame;
	frame.size.height = 480;
	mLoginViewController.view.frame = frame;
	mRouterLocation.latitude = 37.0f;
	mRouterLocation.longitude = -20.0f;

	/*  */
	CGRect webFrame = CGRectMake(14, 70, 300, 240);
	NSString *css = [NSString stringWithFormat:@"<style type=\"text/css\"> body {font-family:helvetica; font-size:10px } a {color: #416297;} ol  {font-weight: bold; margin: 12; padding: 0; list-style-type: decimal} li {margin-bottom:2px;} span {font-weight: normal;} </style>"];
	
	aboutRouterWebView = [[UIWebView alloc] initWithFrame:webFrame];
	[aboutRouterWebView loadHTMLString:[NSString stringWithFormat:@"<html><head>%@</head><body>%@</body></html>", css,
										NSLocalizedString(@"about_text_router_config", @"Router text")] baseURL:nil];
	[(UIScrollView*)[aboutRouterWebView.subviews objectAtIndex:0] setScrollEnabled:NO];
	
	aboutClientWebView = [[UIWebView alloc] initWithFrame:webFrame];
	[aboutClientWebView loadHTMLString:[NSString stringWithFormat:@"<html><head>%@</head><body>%@</body></html>", css,
										NSLocalizedString(@"about_text_client_config", @"Client text")] baseURL:nil];
	[(UIScrollView*)[aboutClientWebView.subviews objectAtIndex:0] setScrollEnabled:NO];
	
	aboutGeneralWebView = [[UIWebView alloc] initWithFrame:webFrame];
	[aboutGeneralWebView loadHTMLString:[NSString stringWithFormat:@"<html><head>%@</head><body>%@</body></html>", css,
										NSLocalizedString(@"about_text_router_general", @"General text")] baseURL:nil];
	[(UIScrollView*)[aboutGeneralWebView.subviews objectAtIndex:0] setScrollEnabled:YES];
	[(UIScrollView*)[aboutGeneralWebView.subviews objectAtIndex:0] setBounces:NO];
	aboutGeneralWebView.dataDetectorTypes = UIDataDetectorTypeLink;
	
	aboutLicenseWebView = [[UIWebView alloc] initWithFrame:webFrame];
	[aboutLicenseWebView loadHTMLString:[NSString stringWithFormat:@"<html><head>%@</head><body>%@</body></html>", css,
										NSLocalizedString(@"about_text", @"This is an application used for controlling a remote powerswitch connected to your Dovado-router Header")] baseURL:nil];
	[(UIScrollView*)[aboutLicenseWebView.subviews objectAtIndex:0] setScrollEnabled:NO];
	aboutLicenseWebView.dataDetectorTypes = UIDataDetectorTypeLink;
	
	//sleep(2); //show splash for this many seconds.
	[NSThread sleepForTimeInterval:2.0]; 
	
// Add the tab bar controller's current view as a subview of the window
//    [window addSubview:tabBarController.view];
	[window addSubview:mLoginViewController.view];
    [window makeKeyAndVisible];
	return YES;
}

- (void) loginButtonClicked {
	[self setJustLoggedIn:TRUE];
	[mLoginViewController.view removeFromSuperview];
	[window addSubview:tabBarController.view];
	[tabBarController setSelectedIndex:0];
}

- (void) storeSharedPrefs {
	//store the input in shared prefs
	NSUserDefaults* userSettings = [NSUserDefaults standardUserDefaults];
	[userSettings setObject:mHostAddress forKey:@"HOST_NAME_PREF"];
	[userSettings setObject:mUser forKey:@"USER_PREF"];
	[userSettings setObject:mPassword forKey:@"PASSWORD_PREF"];
}

- (BOOL) extendedFeatures {
	if (router == ROUTER_UMR || router == ROUTER_4GR) {
		return TRUE;
	}
	return FALSE;	
}

/*
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}
*/

/*
// Optional UITabBarControllerDelegate method
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
}
*/


- (void)dealloc {
	[mHostAddress release];
	[mUser release];
	[mPassword release];
    [tabBarController release];
	[aboutNavigationController release];
	[mLoginViewController release];
    [window release];
    [super dealloc];
}

@end

